#include "com_android_camara_NativeLib.h"


JNIEXPORT jintArray JNICALL Java_com_android_camara_NativeLib_convertToGray
  (JNIEnv * env, jobject obj, jintArray pixels){

	jint i,color=0;
	jint r,g,b=0;
	jint f=0;

	jsize len=(*env)->GetArrayLength(env,pixels);
	jint *colorPixels=(*env)->GetIntArrayElements(env,pixels,0);//a recorrer

	for(i=0;i<len;i++){
			color=colorPixels[i];
			r = (color >> 16) & 0xFF;
			g = (color >> 8) & 0xFF;
			b = (color) & 0xFF;

			f = (int) ((.2125 * r) + (.7154 * g) + (.072 * b));

			colorPixels[i] = (f << 16) | (f << 8) | (f);	
	}

	(*env)->ReleaseIntArrayElements(env, pixels, colorPixels, 0);
   	
   return pixels; 
}


JNIEXPORT jintArray JNICALL Java_com_android_camara_NativeLib_convertToNegative
  (JNIEnv * env, jobject obj, jintArray grayPixels){

	jint i,color=0;
	jint r=0;
	jint f=0;
	jint maxColor=0;



	jsize len=(*env)->GetArrayLength(env,grayPixels);

	jint *inversePixels=(*env)->GetIntArrayElements(env,grayPixels,0);

		for (i = 0; i < len; i++) {

			color = inversePixels[i];
			r = (color >> 16) & 0xFF;
			if (maxColor < r) {
				maxColor = r;
			}
		}

		for (i = 0; i < len; i++) {
			color = inversePixels[i];
			r = (color >> 16) & 0xFF;
			r = maxColor - r;

			inversePixels[i] = (r << 16) | (r << 8) | (r);
		}
	(*env)->ReleaseIntArrayElements(env, grayPixels, inversePixels, 0);

return grayPixels;

}

